package in.muton.edushare.auth;

import in.muton.edushare.EduShareRequestCycle;
import in.muton.edushare.EduShareSession;
import in.muton.edushare.url.URLLoader;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.Component;
import org.apache.wicket.RestartResponseAtInterceptPageException;
import org.apache.wicket.authorization.Action;
import org.apache.wicket.authorization.IAuthorizationStrategy;
import org.apache.wicket.authorization.IUnauthorizedComponentInstantiationListener;

import com.edushare.common.constants.URLConstants;

public class EduShareAuthorizationStrategy implements IAuthorizationStrategy,
		IUnauthorizedComponentInstantiationListener {

	private static Log log = LogFactory.getLog(EduShareAuthorizationStrategy.class);
	public boolean isActionAuthorized(Component arg0, Action arg1) {
		// TODO Auto-generated method stub
		return true;
	}

	public <T extends Component> boolean isInstantiationAuthorized(Class<T> componentClass) {
		if(EduShareAuthenticatedPage.class.isAssignableFrom(componentClass)){
			log.debug("Accessing an authentication-required page.");
			return EduShareSession.get().isAuthenticated();
		}
		return true;
	}

	public void onUnauthorizedInstantiation(Component component) {
		EduShareRequestCycle.get().setRedirect(true);
		throw new RestartResponseAtInterceptPageException(
					URLLoader.getPageClass(URLConstants.LOGIN_PATH));
	}

}
